package likou;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/02/02 08:51
 */
public class _424 {
    public static void main(String[] args) {
        System.out.println(characterReplacement("ABAA",0));
    }
    public static int characterReplacement(String s, int k) {
        int index = 0;
        byte[] bytes = s.getBytes();
        int daan = 1;
        while(index<bytes.length){
            int now = 0;
            while(index<bytes.length-1&&bytes[index+1]==bytes[index]){
                ++index;
                now++;
            }
            daan = Math.max(deep(bytes,k,index,bytes[index],now),daan);
            index++;
            if(daan > bytes.length-index+1){
                return daan;
            }
        }
        return daan;
    }
    public static int deep(byte[] bytes,int k,int index,byte now,int daan){
        int nowFront = index-daan;
        while(index<bytes.length&&(bytes[index]==now||k-->0)){
            index++;
            daan++;
        }
        if(k>0&&nowFront>0){
            daan+=(Math.min(nowFront, k));
        }
        return daan;
    }
}
